import {
  RebateModel
} from '../../../../models/rebate'
const App =  getApp();

const rebateModel = new RebateModel()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusList: ['全部','待结算', '已结算', '已取消'],
    winHeight: "",
    currentTab: 0,
    orderInfoVoList:null,
    pageIndex: 1,
    pageSize: 4,
    loading: false,
    hasNext: true,
    empty:false,
    statusBarHeight: App.globalData.statusBarHeight,
    isTabbar:false
  },

  tapToNiv: function (event) {
    //获取当前tab栏目id
    const tid = event.currentTarget.dataset.id
    if(tid == this.data.currentTab) {
      return 
    }
    wx.pageScrollTo({
        scrollTop: 0,
        duration:0
      })
    this.getRebateDefault(tid)
  },

  
  getRebateDefault: function (tid) {
    this.data.pageIndex = 1
    const params = {
      type: tid,
      pageIndex: this.data.pageIndex,
      pageSize: this.data.pageSize,
    }
    const self = this;
    rebateModel.getRebateDetails(params).then(res => {
      if (res.data.orderInfoVoList.length == 0) {
        self.data.empty = true
      } else {
        self.data.empty = false
      }
      self.setData({
        empty:self.data.empty,
        currentTab: tid,
        orderInfoVoList: res.data.orderInfoVoList,
        hasNext: res.data.hasNext,
        loading: false
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },

  initData:function(){
    this.getRebateDefault(this.data.currentTab)
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.initData()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      pageIndex:1, 
      pageSize: 4
    })
    this.initData()
    wx.stopPullDownRefresh()
  },

  //加载更多
  loadMore(isPull) {
    console.log(this.data.pageIndex)
    if (this.data.loading) {
      return
    }
    if (this.data.hasNext) {
      const params = {
        type: this.data.currentTab,
        pageIndex: this.data.pageIndex,
        pageSize: this.data.pageSize,
      }
      if (isPull) {
        console.log(isPull)
        this.setData({
          loading: true
        })
      }
      rebateModel.getRebateDetails(params).then(res => {
        const tempArray = this.data.orderInfoVoList.concat(res.data.orderInfoVoList)
        console.log(res)
        this.setData({
          orderInfoVoList: tempArray,
          hasNext: res.data.hasNext,
          loading: false
        })
      })
    }
  },


  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(!this.data.hasNext){
      return
    }
    this.data.pageIndex = this.data.pageIndex + 1
    this.loadMore(true)
  },

})